<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入查询内容"
            v-model="queryinfo.query"
            size="small"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="query"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-row>
        <el-table class="tabletop" :data="tableData" border style="width: 100%">
          <el-table-column type="index" label="序号"></el-table-column>
          <el-table-column
            prop="order_number"
            label="订单编号"
          ></el-table-column>
          <el-table-column
            prop="order_price"
            label="订单价格"
          ></el-table-column>
          <el-table-column label="是否付款">
            <template slot-scope="scope">
              <el-tag type="danger" v-show="scope.row.order_pay == 0"
                >未付款</el-tag
              >
              <el-tag v-show="scope.row.order_pay == 1">支付宝支付</el-tag>
              <el-tag type="success" v-show="scope.row.order_pay == 2"
                >微信支付</el-tag
              >
              <el-tag type="warning" v-show="scope.row.order_pay == 3"
                >银行卡支付</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="is_send" label="是否发货"></el-table-column>
          <el-table-column label="下单时间">
            <template slot-scope="scope">
              <!-- 过滤器在指令中的使用方式 -->
              <div
                v-html="$options.filters.fmdtime(scope.row.create_time)"
              ></div>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150">
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="goeait(row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryinfo.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalsize"
        ></el-pagination>
      </el-row>
    </el-card>
    <!-- 编辑弹框 -->
    <el-dialog title="编辑订单" :visible.sync="dialogVisible" width="50%">
      <el-form :model="searchObj" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户ID">
          <el-input v-model="searchObj.user_id" disabled></el-input>
        </el-form-item>
        <el-form-item label="支付状态">
          <el-input v-model="searchObj.pay_status" disabled></el-input>
        </el-form-item>
        <el-form-item label="是否发货">
          <el-input v-model="searchObj.is_send" disabled></el-input>
        </el-form-item>
        <el-form-item label="订单价格">
          <el-input v-model="searchObj.order_price"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "orders",
  filters: {
    fmdtime: function (value) {
      const newdate = new Date(value);
      let str =
        newdate.getFullYear() +
        "-" +
        (newdate.getMonth() + 1) +
        "-" +
        newdate.getDate() +
        "<br/>" +
        newdate.getHours() +
        ":" +
        newdate.getMinutes() +
        ":" +
        newdate.getSeconds();
      return str;
    },
  },
  data() {
    return {
      dialogVisible: false,
      searchObj: {
        user_id: "",
        pay_status: "",
        is_send: "",
        order_price: "",
        order_fapiao_title: "",
        order_fapiao_company: "",
        order_fapiao_content: "",
        consignee_addr: "",
      },
      tableData: [], //定义表格数据源
      // 定义查询接口所携带的参数
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      // 定义总的数据条数
      totalsize: 0,
    };
  },
  methods: {
    // 编辑
    goeait(row) {
      this.dialogVisible = true;
      this.searchObj = { ...row };
    },
    async update() {
      let rst = await this.$http.put(
        `orders/${this.searchObj.order_id}`,
        this.searchObj
      );
      if (rst.meta.status == 201) {
        this.dialogVisible = false;
        this.$message.success(rst.meta.msg);
        this.query();
      }
    },
    // 当每页显示的数据条数发生变化时调用的方法,该方法接收一个参数,值为当前每页显示的数据条数
    handleSizeChange(size) {
      this.queryinfo.pagesize = size;
      this.query();
    },
    // 当点击页码时触发的方法,该方法接收一个参数,参数表示当前页码
    handleCurrentChange(pagenum) {
      this.queryinfo.pagenum = pagenum;
      this.query();
    },
    // 定义查询方法
    async query() {
      let rst = await this.$http.get("orders", this.queryinfo);
      if (rst.meta.status == 200) {
        this.tableData = rst.data.goods; //为表格数据源赋值
        this.totalsize = rst.data.total; //为总数据条数赋值
      } else {
        this.$message.error("数据请求失败");
      }
    },
  },
  mounted() {
    // 调用查询方法
    this.query();
  },
};
</script>

<style>
</style>
